/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package library;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author Administrator
 */
public class TestLibraryFrame extends javax.swing.JFrame {

    /**
     * Creates new form TestLibraryFrame
     */
    ResultSet rs = null;       
    PreparedStatement pst=null;
     
    public TestLibraryFrame() {
        initComponents();
        test();
    }
     private void test()
    {
         System.out.println("test!!!!");
          try
            {
                System.err.println("UPDATE METHOD CALLED!!!!");
                System.out.println("UPDATE METHOD CALLED!!!!");
                String dbUrl = "jdbc:mysql://localhost/collegeaccountant";
                Class.forName("com.mysql.jdbc.Driver");
                Connection con=DriverManager.getConnection(dbUrl,"root","mysql");
                System.err.println("Connected for Update ... 1111!!!!");
                String sql = "select * from library";
                pst = con.prepareStatement(sql);               
                rs = pst.executeQuery();
                System.out.println("Before Table Library!!!!"+sql);
                LibraryTable.setModel(DbUtils.resultSetToTableModel(rs));
                System.out.println("After Table Library!!!!!");
            }
            catch (Exception e)
            {
                System.err.println("Not Connected UPDATION!!!!"+e.getMessage());
                System.out.println("Not Connected UPDATION!!!!"+e.getMessage());           
            }
            finally
            {
                try {
                    rs.close();
                    pst.close();
                } 
                catch (SQLException ex) {
                    Logger.getLogger(FrameLibraryModule.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_StudentID = new javax.swing.JLabel();
        Label_Vendor = new javax.swing.JLabel();
        txt_StudentID = new javax.swing.JTextField();
        txt_Vendor = new javax.swing.JTextField();
        btn_Update = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        LibraryTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        label_StudentID.setText("Student ID");

        Label_Vendor.setText("Vendor");

        btn_Update.setText("UPDATE");
        btn_Update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_UpdateActionPerformed(evt);
            }
        });

        LibraryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        LibraryTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LibraryTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LibraryTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(label_StudentID, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                            .addComponent(Label_Vendor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_StudentID)
                            .addComponent(txt_Vendor, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(btn_Update, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_StudentID, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_StudentID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Label_Vendor)
                            .addComponent(txt_Vendor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addComponent(btn_Update))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LibraryTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LibraryTableMouseClicked
          try
       {
            String dbUrl = "jdbc:mysql://localhost/collegeaccountant";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection(dbUrl,"root","mysql");
            System.err.println("Connected ... 1111!!!!");
            int row = LibraryTable.getSelectedRow();
            String Table_Click = (LibraryTable.getModel().getValueAt(row, 0).toString());
            String sql = " select * from library where StudentID = '"+Table_Click+"'";
            System.out.println("value of Table Click!!!!"+Table_Click);
            pst = con.prepareStatement (sql);
	    rs=pst.executeQuery(sql);
            if(rs.next())
            {
               System.out.println("Value populated");
               String add1 = rs.getString("StudentID");
               txt_StudentID.setText(add1);            
           
               
               String add3 = rs.getString("Vendor");
               txt_Vendor.setText(add3);
               System.err.println(" Connected...2222!!!!"+add1+" second Value"+add3);
           }
                  
       }
       catch (Exception e)
       {
           System.err.println("Not Connected!!!!"+e.getMessage());
            System.out.println("Not Connected!!!!"+e.getMessage());
           
       }
         finally
            {
                try {
                    rs.close();
                    pst.close();
                } 
                catch (SQLException ex) {
                    System.err.println("SQL EXCEPTION");
                    Logger.getLogger(FrameLibraryModule.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    }//GEN-LAST:event_LibraryTableMouseClicked

    private void btn_UpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_UpdateActionPerformed
       try
       {
            String dbUrl = "jdbc:mysql://localhost/collegeaccountant";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection(dbUrl,"root","mysql");
            System.err.println("Connected for Update ... 1111!!!!");
           
            String studentID = txt_StudentID.getText();
            String vendor =  txt_Vendor.getText();

            String sql = "update library set Vendor = '"+vendor+"' where StudentID = '"+studentID+"'";
            pst = con.prepareStatement(sql);
            pst.execute();
            System.out.println("Update Query!!!!"+sql);
            JOptionPane.showMessageDialog(null,"tableUpdated");
                  
       }
       catch (Exception e)
       {
           System.err.println("Not Connected UPDATION!!!!"+e.getMessage());
           System.out.println("Not Connected UPDATION!!!!"+e.getMessage());
           
       }
            finally
            {
                try {
                    rs.close();
                    pst.close();
                } 
                catch (SQLException ex) {
                    System.err.println("SQL EXCEPTION");
                    Logger.getLogger(FrameLibraryModule.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
       test();
    }//GEN-LAST:event_btn_UpdateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestLibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestLibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestLibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestLibraryFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestLibraryFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Label_Vendor;
    private javax.swing.JTable LibraryTable;
    private javax.swing.JButton btn_Update;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_StudentID;
    private javax.swing.JTextField txt_StudentID;
    private javax.swing.JTextField txt_Vendor;
    // End of variables declaration//GEN-END:variables
}
